package com.example.carrierapp.ui.guidedialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Message
import com.example.carrierapp.config.gone
import com.example.carrierapp.config.show
import com.example.carrierapp.util.DialogUtils
import com.example.carrierapp.util.SPUtils
import com.example.lb.fingerdemo.R
import kotlinx.android.synthetic.main.dialog_guide.*

class GuideDialog(context: Context) : Dialog(context, R.style.Dialog) {

    private lateinit var handler: Handler

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_guide)
        handler = @SuppressLint("HandlerLeak")
        object : Handler() {
            override fun handleMessage(msg: Message?) {
                when (msg?.obj.toString().toInt()) {
                    0 -> {
                        dispatch_guide_bottom.gone()
                        dispatch_guide_top.gone()
                        waybill_guide.gone()
                        goods_guide.show()
                    }
                    1 -> {
                        dispatch_guide_bottom.show()
                        dispatch_guide_top.show()
                        waybill_guide.gone()
                        goods_guide.gone()
                    }
                    2 -> {
                        dispatch_guide_bottom.gone()
                        dispatch_guide_top.gone()
                        goods_guide.gone()
                        waybill_guide.show()
                    }
                    3 -> {
                        add_dispatch_guide.show()
                    }
                }
            }
        }
        root.setOnClickListener { dismiss() }
        DialogUtils.initDialogWidthAndHeight(window, 1f, 1f)
    }

    fun show(position: Int) {
        if (SPUtils.getBoolean(context, "show_guide_$position", false).not()) {
            super.show()
            handler.sendMessage(handler.obtainMessage(0x01, position))
            SPUtils.putBoolean(context, "show_guide_$position", true)
        }
    }
}